import config from "./../config";

//utils为工具类
export default {
    setItem (key, value) {
        const storage = this.getStorage()
        storage[key] = value
        window.localStorage.setItem(config.namespace, JSON.stringify(storage))
    },
    getItem (key) {
        return this.getStorage()[key]
    },
    clearItem (key) {
        //按照key名删除item
        const storage = this.getStorage()
        delete storage[key]
        //相当于刷新
        window.localStorage.setItem(config.namespace,JSON.stringify(storage))
    },
    clearAll () {
        //将localStorage中的数据全部清除
        window.localStorage.clear()
    },
    getStorage () {
        //从localstorage中获得数据
        return JSON.parse(window.localStorage.getItem(config.namespace) || '{}')
        //获取不到则返回空列表
    }
}